package com.eboss2.sbs.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.eboss2.sbs.R;
import com.eboss2.sbs.activity.base.BaseActivity;
import com.eboss2.sbs.http.HttpUtil;
import com.loopj.android.http.RequestParams;
import com.mytools.tool.activityManager.MyActivityManager;
import com.tool.utils.utils.StringUtils;
import com.tool.utils.utils.ToastUtils;
import com.tool.utils.utils.UtilPreference;

import org.json.JSONObject;

import java.text.ParseException;

/**
 * Created by Administrator on 2018/2/1 0001.
 */

public class ResetOperatorPwdActivity extends BaseActivity implements View.OnClickListener{
    private final String TAG = "ResetOperatorPwdActivity";
    private EditText et_new_pwd;
    private EditText et_comfirm_new_pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_reset_operator_pwd);
        MyActivityManager.getInstance().addActivity(this);
        initView();
    }

    private void initView() {

        showView(R.id.titleback_btn_back, true);
        findViewById(R.id.titleback_btn_back).setOnClickListener(this);
        setTvText(R.id.tv_title, "重置密码");
        showView(R.id.next_sure, false);

        et_new_pwd = editText(R.id.et_new_pwd);
        et_comfirm_new_pwd = editText(R.id.et_comfirm_new_pwd);

        button(R.id.btn_commit).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.titleback_btn_back:
                finish();
                break;
            case R.id.btn_commit:

                break;
        }
    }



    /**
     * 提交
     */
    private void doCommit() throws ParseException {




        if (StringUtils.isBlank(et_new_pwd.getText().toString().trim())) {
            ToastUtils.showShort(this, "新密码不能为空");
            et_new_pwd.requestFocus();
            return;
        }

        if (StringUtils.isBlank(et_comfirm_new_pwd.getText().toString().trim())) {
            ToastUtils.showShort(this, "确认密码不能为空");
            et_comfirm_new_pwd.requestFocus();
            return;
        }

        if (!StringUtils.isEquals(et_new_pwd.getText().toString().trim(), et_comfirm_new_pwd.getText().toString().trim())) {
            ToastUtils.showShort(this, "两次输入密码不一致");
            return;
        }



        showProgress("加载中...");
        RequestParams params = new RequestParams();
        params.add("memberId", UtilPreference.getStringValue(mContext, "memberId"));
        params.add("token", UtilPreference.getStringValue(mContext, "token"));
        params.add("phone", UtilPreference.getStringValue(mContext, "userName"));
        params.add("password", et_new_pwd.getText().toString().trim());
        params.add("type", "2");
//        HttpUtil.get(ConfigXy.XY_FORGOT_PAY_PWD, params, requestListener);

    }

    private HttpUtil.RequestListener requestListener = new HttpUtil.RequestListener() {

        @Override
        public void success(String response) {
            disShowProgress();
            try {
                JSONObject result = new JSONObject(response);
                if (result.optInt("code") == -2){
                    UtilPreference.clearNotKeyValues(mContext);
                    // 退出账号 返回到登录页面
                    MyActivityManager.getInstance().logout(mContext);
                    return;
                }
                if (!result.optBoolean("status")) {
                    ToastUtils.CustomShow(mContext, result.optString("message"));
                }else{
                    ToastUtils.CustomShow(mContext, result.optString("message"));
                    finish();
                }



            } catch (Exception e) {
                Log.e(TAG, "doCommit() Exception: " + e.getMessage());
            }
        }

        @Override
        public void failed(Throwable error) {
            disShowProgress();
            showE404();
        }
    };
}
